/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.xwc.example.slice;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.github.xwc.example.ResourceTable;
import com.github.xwc.example.tab.DynamicPagerIndicator;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.PageSlider;

import java.util.ArrayList;
import java.util.List;

/**
 * MainAbilitySlice
 *
 * @since 2021-03-04
 */
public class MainAbilitySlice extends AbilitySlice {
    private static final int DELAY_TIME = 100;
    private String mImageUrl;
    private DynamicPagerIndicator dynamicPagerIndicator;
    private PageSlider viewPager;
    private PageSliderProviderUtil adapter;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        dynamicPagerIndicator = (DynamicPagerIndicator) findComponentById(ResourceTable.Id_dynamic_pager_indicator7);
        viewPager = (PageSlider) findComponentById(ResourceTable.Id_view_pager7);

        adapter = new PageSliderProviderUtil(getComponentList(), getTitleList());
        viewPager.setProvider(adapter);
        dynamicPagerIndicator.getPagerTabView(0);
        dynamicPagerIndicator.setViewPager(viewPager);

        dynamicPagerIndicator.setScrollLine();
        getUITaskDispatcher().delayDispatch(new Runnable() {
            @Override
            public void run() {
                viewPager.setCurrentPage(0);
            }
        }, DELAY_TIME);

        mImageUrl = getContext().getString(ResourceTable.String_imgUrl);
        Image urlImage = (Image) findComponentById(ResourceTable.Id_circleImage3);
        Glide.with(getContext())
            .load(mImageUrl)
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .skipMemoryCache(false)
            .into(urlImage);
    }

    private List<Component> getComponentList() {
        List<Component> componentList = new ArrayList<>();
        Component rootView1 = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_fragment_example1, null, true);
        componentList.add(rootView1);

        Component rootView2 = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_fragment_example2, null, true);
        componentList.add(rootView2);

        Component rootView3 = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_fragment_example3, null, true);
        componentList.add(rootView3);
        return componentList;
    }

    private List<String> getTitleList() {
        List<String> title = new ArrayList<>();
        title.add("DEMO1");
        title.add("DEMO2");
        title.add("DEMO3");
        return title;
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
